//
//  DDHomeController.m
//  NetEaseNews
//
//  Created by dqw on 15/7/24.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "DDHomeController.h"
#import "DDChannel.h"
#import "DDChannelCell.h"
#import "DDChannelLabel.h"
//#import "HMChannel.h"
@interface DDHomeController ()<UICollectionViewDataSource, UIScrollViewDelegate>
@property (strong, nonatomic) NSArray *channels;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (assign, nonatomic) NSInteger currentIndex;



@end

@implementation DDHomeController

static NSString * const reuseID = @"channel";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configurateCollectionView];
//    self.channels = [HMChannel channels];
//    self.channels = [DDChannel channels];
//    [self loadChannelLabels];
}
- (NSArray *)channels
{
    if (!_channels) {
        _channels = [DDChannel channels];
    }
    return _channels;
}

- (void)configurateCollectionView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
}
- (void)loadChannelLabels
{
    CGFloat Xmargin = 5;
    CGFloat Height = self.scrollView.bounds.size.height;
    CGFloat X = Xmargin;
    for (id obj in self.scrollView.subviews) {
        [obj removeFromSuperview];
    }
    for (DDChannel *channel in self.channels) {
        DDChannelLabel *label = [DDChannelLabel channelLabelWithName:channel.tname];
        [self.scrollView addSubview:label];
        label.frame = CGRectMake(X, 0, label.bounds.size.width, Height);
        X += Xmargin + (label.bounds.size.width + Xmargin);
    }
    self.scrollView.contentSize = CGSizeMake(X, 0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    DDChannelLabel *firstLabel = self.scrollView.subviews[0];
    firstLabel.scale = 1;
    
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.flowLayout.itemSize = self.collectionView.bounds.size;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.channels.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DDChannelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
    
    cell.channel = self.channels[indexPath.item];
    return cell;
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    CGFloat curScale = scrollView.contentOffset.x / self.collectionView.bounds.size.width - self.currentIndex;
//    CGFloat nexScale = 1 - curScale;
//    
//    NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
//    DDChannelLabel *curLabel, *nexLabel;
//    for (NSIndexPath *indexPath in indexPaths) {
//        if (indexPath.item == self.currentIndex) {
//            curLabel = self.scrollView.subviews[indexPath.item];
//        }else
//        {
//            nexLabel = self.scrollView.subviews[indexPath.item];
//        }
//    }
//    curLabel.scale = curScale;
//    nexLabel.scale = nexScale;
//}
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    self.currentIndex = scrollView.contentOffset.x / scrollView.bounds.size.width;
//}

@end
